# -*- coding: utf-8 -*
# @author 小测试
# @create 2020-03-13 14:09
from ApiDemo.util.get_log import MyLog
import json


class OpenJson:

    def __init__(self, file_path):
        self.log = MyLog
        self.file_path = file_path
        self.data = self.read_data()

    # 读取json文件
    def read_data(self):
        self.log.info("读取接口数据--->{}".format(self.file_path))
        if self.file_path == None:
            with open(self.file_path, encoding="utf-8") as fp:
                data = json.load(fp)
                return data
        else:
            with open(self.file_path, encoding="utf-8") as fp:
                data = json.load(fp)
                return data

    # 根据关键字获取数据
    def get_data(self, id):
        return self.data[id]

    # 写json
    def write_data(self, data):
        with open(self.file_path, 'w') as fp:
            fp.write(json.dumps(data))


if __name__ == '__main__':
    user_path="../../cases/user/user_api_data.json"
    openjson = OpenJson(user_path)
    print(openjson.read_data())
